#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#define MAX_CUSTOMERS 100

typedef struct
{
    int id;
    char name[50];
    char gender[10];
    int age;
    char phone[20];
    char email[50];
} Customer;

Customer customers[MAX_CUSTOMERS];
int customerCount = 0;

void displayMenu()
{
    printf("---------客户信息管理软件---------\n");
    printf("1 添加客户\n");
    printf("2 修改客户\n");
    printf("3 删除客户\n");
    printf("4 客户列表\n");
    printf("5 退出\n");
    printf("请选择(1-5): ");
}

void addCustomer()
{
    if (customerCount >= MAX_CUSTOMERS)
    {
        printf("客户数量已达上限\n");
        return;
    }
    printf("--------------添加客户--------------\n");
    customers[customerCount].id = customerCount + 1;
    printf("姓名: ");
    scanf("%s", customers[customerCount].name);
    printf("性别: ");
    scanf("%s", customers[customerCount].gender);
    printf("年龄: ");
    scanf("%d", &customers[customerCount].age);
    printf("电话: ");
    scanf("%s", customers[customerCount].phone);
    printf("邮箱: ");
    scanf("%s", customers[customerCount].email);
    customerCount++;
    printf("--------------添加完成--------------\n");
}

void listCustomers()
{
    if (customerCount == 0) {
        printf("没有客户信息\n");
        return;
    }
    printf("--------------客户列表--------------\n");
    printf("编号\t姓名\t性别\t年龄\t电话\t\t邮箱\n");
    for (int i = 0; i < customerCount; i++)
    {
        printf("%d\t%s\t%s\t%d\t%s\t%s\n", 
               customers[i].id, customers[i].name, customers[i].gender, 
               customers[i].age, customers[i].phone, customers[i].email);
    }
}

void modifyCustomer()
{
    int id;
    printf("--------------修改客户--------------\n");
    printf("请选择待修改客户编号(-1退出): ");
    scanf("%d", &id);
    if (id == -1)
    {
        return;
    }
    int index = -1;
    for (int i = 0; i < customerCount; i++)
    {
        if (customers[i].id == id)
        {
            index = i;
            break;
        }
    }

    if (index != -1)
    {
        printf("姓名(%s): ", customers[index].name);
        scanf("%s", customers[index].name);
        printf("性别(%s): ", customers[index].gender);
        scanf("%s", customers[index].gender);
        printf("年龄(%d): ", customers[index].age);
        scanf("%d", &customers[index].age);
        printf("电话(%s): ", customers[index].phone);
        scanf("%s", customers[index].phone);
        printf("邮箱(%s): ", customers[index].email);
        scanf("%s", customers[index].email);
        printf("--------------修改完成--------------\n");
        printf("--------------修改成功!--------------\n");
    } else {
        printf("未找到该编号客户\n");
    }
}

void deleteCustomer()
{
    int id;
    printf("--------------删除客户--------------\n");
    printf("请选择待删除客户编号(-1退出): ");
    scanf("%d", &id);
    if (id == -1)
    {
        return;
    }
    int index = -1;
    for (int i = 0; i < customerCount; i++)
    {
        if (customers[i].id == id)
        {
            index = i;
            break;
        }
    }
    if (index != -1)
    {
        char confirm;
        printf("确认是否删除(Y/N): ");
        scanf(" %c", &confirm);
        if (confirm == 'Y' || confirm == 'y')
        {
            for (int i = index; i < customerCount - 1; i++)
            {
                customers[i] = customers[i + 1];
            }
            customerCount--;
            printf("--------------删除成功!!--------------\n");
        }
    } else{
        printf("未找到该编号客户\n");
    }
}

int main()
{
    int choice;
    while (1)
    {
        displayMenu();
        scanf("%d", &choice);
        switch (choice)
        {
            case 1:
                addCustomer();
                break;
            case 2:
                modifyCustomer();
                break;
            case 3:
                deleteCustomer();
                break;
            case 4:
                listCustomers();
                break;
            case 5:
            {
                char confirm;
                printf("确认是否退出(Y/N): ");
                scanf(" %c", &confirm); // 注意前面的空格，跳过换行符
                if (confirm == 'Y' || confirm == 'y') {
                    printf("你退出了客户管理系统\n");
                    return 0; // 正常退出程序
                } else {
                    printf("退出操作已取消。\n");
                }
                break;
            }
            default:
                printf("无效的选择，请重新输入\n");
        }
    }
    return 0;
}